package com.gupao.javavip.netty.push.protocol;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
import lombok.extern.apachecommons.CommonsLog;
import org.msgpack.MessagePack;

import java.util.Arrays;

@CommonsLog
public class AbstractMessagePackEncoder<T> extends MessageToByteEncoder<T> {
    private MessagePack messagePack = new MessagePack();

    @Override
    protected void encode(ChannelHandlerContext ctx, T obj, ByteBuf out) throws Exception {
        log.debug("发送消息：" + obj.toString());
        byte[] content = messagePack.write(obj);
        log.debug("encode Str:" + Arrays.toString(content));
        out.writeBytes(content);
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        log.warn("与客户端断开连接:" + cause.getMessage(), cause);
        ctx.close();
    }
}
